package org.example;

import org.example.entity.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.internal.AbstractServiceRegistryImpl;
import org.hibernate.service.internal.SessionFactoryServiceRegistryBuilderImpl;
import org.hibernate.service.internal.SessionFactoryServiceRegistryImpl;
import org.junit.Test;

import java.util.Date;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-21 15:23
 * @Description
 */
public class Demo1 {
    @Test
    public void test1() {
        //1. 创建 sessionFactory
        SessionFactory sessionFactory = null;
        //(a)创建 Configuration 对象：对应 hibernate的基本配置信息和对象关系映射信息
        Configuration configure = new Configuration().configure();
        /*
         * public Configuration configure() throws HibernateException {
         *   return this.configure("hibernate.cfg.xml");
         * }
         * */
        //(b)创建 sessionFactory赋值
        sessionFactory = configure.buildSessionFactory();
        //2. 创建 session 对象
        Session session = sessionFactory.openSession();
        //3. 开启事务
        Transaction transaction = session.beginTransaction();
        //4. 执行保存操作
//        Student student = new Student(5, "草原", new Date(), "内蒙");
//        System.out.println(session.save(student));//4
        Student student = (Student) session.get(Student.class, 1);
        System.out.println("student = " + student);
        //5. 提交事务
        transaction.commit();
        //6. 关闭 session
        session.close();
        //7. 关闭 sessionFactory
        sessionFactory.close();
    }

    @Test
    public void test2() {
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session currentSession = sessionFactory.getCurrentSession();
    }


}
